#include "audioVoencodetask.h"
#include <qDebug>
#include <dlfcn.h> /* 必须加这个头文件 */
typedef int (VO_API * VOGETAUDIODECAPI) (VO_AUDIO_CODECAPI *);
AudioEncodeTask::AudioEncodeTask(Receive_handler *pHandler):m_pHandler(pHandler)
{
    outfile = NULL;

    AACENC_PARAM				aacpara;
    VO_MEM_OPERATOR				moper;
    VO_CODEC_INIT_USERDATA		useData;

    aacpara.adtsUsed    = 0;
    aacpara.bitRate     = 16000;//64Kpbs
    aacpara.nChannels   = 1;    //单声道
    aacpara.sampleRate  = 8000; //采样率

    moper.Alloc = cmnMemAlloc;
    moper.Copy = cmnMemCopy;
    moper.Free = cmnMemFree;
    moper.Set = cmnMemSet;
    moper.Check = cmnMemCheck;

    useData.memflag = VO_IMF_USERMEMOPERATOR;
    useData.memData = (VO_PTR)(&moper);

    VOGETAUDIODECAPI			pGetAPI;
    int							returnCode;
    void						*pfunc;

    pSo_handler = dlopen("libvo-aacenc.so", RTLD_NOW);

    // Get API;
    pfunc = dlsym(pSo_handler, "voGetAACEncAPI");
    pGetAPI = (VOGETAUDIODECAPI)pfunc;

    returnCode  = pGetAPI(&AudioAPI);
    if(returnCode)
        return ;

    returnCode = AudioAPI.Init(&hCodec, VO_AUDIO_CodingAAC, &useData);
    qDebug(QString("returnCode1:").append(QString::number((long)returnCode)).toLocal8Bit().data());

    returnCode = AudioAPI.SetParam(hCodec, VO_PID_AAC_ENCPARAM, &aacpara);

    qDebug(QString("returnCode2:").append(QString::number((long)returnCode)).toLocal8Bit().data());

    qDebug(QString("dlopen:").append(QString::number((long)pSo_handler)).toLocal8Bit().data());
    qDebug(QString("voGetAACEncAPI:").append(QString::number((long)pfunc)).toLocal8Bit().data());

    m_pack_num1 = 0;

    m_pack_num2 = 0;
}

AudioEncodeTask::~AudioEncodeTask()
{
    int returnCode = AudioAPI.Uninit(hCodec);

    dlclose(pSo_handler);
}

int AudioEncodeTask::close()
{
    std::string str("");

    int nthread  =thr_count();

    for (int i =0 ;i<nthread ;i++)
        putq(str);

    wait();
    return 0;
}

bool AudioEncodeTask::receive_handler(char *data, int size)
{
    m_pack_num1 ++ ;
    qDebug(QString("AudioEncodeTask::receive_handler size:").append(QString::number((long)size)).toLocal8Bit().data());
    putq(std::string(data,size));
    return true;
}

#define IBUF_SIZE	(2048)
#define OBUF_SIZE	(2048)
int AudioEncodeTask::svc()
{
    std::string str ="";

    unsigned char *pOutBuf  = new unsigned char [OBUF_SIZE];
    unsigned char *pInBuf   = new unsigned char [IBUF_SIZE];

    VO_CODECBUFFER				inData;
    VO_CODECBUFFER				outData;
    VO_AUDIO_OUTPUTINFO			outInfo;

    inData.Buffer   = pInBuf;
    inData.Length   = IBUF_SIZE;
    while(getq(str))
    {
        if (str=="")
            break;

        m_pack_num2 ++;

        int data_size = str.length();

        if(data_size == IBUF_SIZE)
        {
            memcpy(inData.Buffer,str.data(),data_size);

            //qDebug(QString("SetInputData size:").append(QString::number((long)data_size)).toLocal8Bit().data());

            int returnCode = AudioAPI.SetInputData(hCodec,&inData);
            // qDebug(QString("AudioAPI.SetInputData returnCode:").append(QString::number((long)returnCode)).toLocal8Bit().data());

            outData.Buffer  = pOutBuf;
            outData.Length  = OBUF_SIZE;

            returnCode = AudioAPI.GetOutputData(hCodec,&outData, &outInfo);

            qDebug(QString("GetOutputData returnCode:").append(QString::number((long)returnCode)).toLocal8Bit().data());

            if(returnCode == VO_ERR_LICENSE_ERROR)
                break;

            if(m_pHandler)
                 m_pHandler->receive_handler((char*)outData.Buffer,outData.Length);//发送到网络

            //qDebug(QString("xxxxxxxxxxxxxxxxxx000").toLocal8Bit().data());

            //qDebug(QString("m_pHandler->receive_handler size:").append(QString::number((long)outData.Length)).toLocal8Bit().data());

          // fwrite(outData.Buffer, 1, outData.Length, outfile);

        }
            // int nRetLength = pfaacEncEncode(hEncoder, (int*) pbPCMBuffer, nInputs, pbAACBuffer, nMaxOutputBytes);

//             QString strInfo;
//             strInfo.sprintf("pack_num1: %d  pack_num2:%d ",m_pack_num1,m_pack_num2);
//             qDebug(strInfo.toLocal8Bit().data());

//             if(outfile)
//                fwrite(pbAACBuffer, 1, nRetLength, outfile);
     }
     delete[] pOutBuf;
     delete[] pInBuf;

     return 0;
}

